﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ircclient
{
    class Channel : Privat
    {
        private List<string> users = new List<string>();

        private bool endOfUserList = true;
        
        public List<string> Users
        {
            set 
            {
                if (value.Last() == "")
                    value.RemoveAt(value.Count - 1);
                if (endOfUserList)
                {
                    users = value;
                    endOfUserList = false;  
                }
                else
                {
                    users.AddRange(value);
                }
                form.AddUserBox(value.ToArray(), endOfUserList);
            }
            get { return users; }
        }
        public void addUser(string user)
        {
            form.AddUserBox(user);
            users.Add(user);
        }
        public void delUser(string user)
        {
            form.DelUserBox(user);
            users.Remove(user);
        }
        public void EndOfUserList()
        {
            endOfUserList = true;
        }

        public Channel(string name, FormMain owner, Server server) :base(name,owner,server)
        {
            server.onMessage += new EventHandler<Server.MessageArg>(server_onMessage);
            form.OpenUserBox();
        }


        protected void server_onMessage(object sender, Server.MessageArg e)
        {
            string SourceNick = e.Source.Split('!')[0];
            if (String.Compare(e.Target, name, true) == 0)
            {
                if (e.Type == typeMessage.chat)
                    OutMessage(SourceNick, e.Message);
                else if (e.Type == typeMessage.chatnotice)
                    OutNotice(SourceNick, e.Message);
                else if (e.Type == typeMessage.ctcp || e.Type == typeMessage.ctcpnotice)
                    OutNotice(SourceNick, e.Message);
            }
        }
    }
}
